import { director, _decorator, Component, Node, v3, UITransform, Collider2D, Contact2DType } from "cc";
import { setLastPosition } from "./lastPosition";

const { ccclass, property } = _decorator;

@ccclass("text")
export class text extends Component {
  start() {
    const ui = this.node.parent.getComponent(UITransform);
    this.node.on(Node.EventType.TOUCH_MOVE, (e) => {
      const u = e.getUILocation();
      const v = ui.convertToNodeSpaceAR(v3(u.x, u.y, 0));
      setLastPosition(u);
      this.node.setPosition(v);
    });
    const collider = this.node.getComponent(Collider2D);
    collider.on(Contact2DType.BEGIN_CONTACT, this.stayInRect, this);
    // collider.on(Contact2DType.END_CONTACT, this.stayInRect, this);
    // c.on("onCollisionStay", this.stayInRect, this);
    // c.on("onCollisionEnter", this.stayInRect, this);
    // c.on(Contact2DType.BEGIN_CONTACT, this.stayInRect, this);
    // c.on(Contact2DType.POST_SOLVE, this.stayInRect, this);
    // c.on("onCollisionStay", this.stayInRect, this);
  }

  stayInRect(self, other, contact) {
    this.node.setParent(other.node);
  }
  update(deltaTime: number) {}
}
